/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.continew.admin.card.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import top.continew.admin.card.mapper.BusinessCardVisitorRecordMapper;
import top.continew.admin.card.model.entity.BusinessCardVisitorRecordDO;
import top.continew.admin.card.model.query.BusinessCardVisitorRecordQuery;
import top.continew.admin.card.model.req.BusinessCardVisitorRecordReq;
import top.continew.admin.card.model.resp.BusinessCardVisitorRecordDetailResp;
import top.continew.admin.card.model.resp.BusinessCardVisitorRecordResp;
import top.continew.admin.card.model.resp.CardVisitorResp;
import top.continew.admin.card.model.resp.VisitorStatisticsRep;
import top.continew.admin.card.service.BusinessCardVisitorRecordService;
import top.continew.starter.extension.crud.model.query.PageQuery;
import top.continew.starter.extension.crud.model.resp.PageResp;
import top.continew.starter.extension.crud.service.impl.BaseServiceImpl;

import java.util.List;

/**
 * 微信名片访问记录业务实现
 *
 * @author floyd
 * @since 2024/12/02 15:48
 */
@Service
@RequiredArgsConstructor
public class BusinessCardVisitorRecordServiceImpl extends BaseServiceImpl<BusinessCardVisitorRecordMapper, BusinessCardVisitorRecordDO, BusinessCardVisitorRecordResp, BusinessCardVisitorRecordDetailResp, BusinessCardVisitorRecordQuery, BusinessCardVisitorRecordReq> implements BusinessCardVisitorRecordService {

    @Override
    public PageResp<BusinessCardVisitorRecordResp> page(BusinessCardVisitorRecordQuery query, PageQuery pageQuery) {
        // 创建一个新的QueryWrapper对象
        QueryWrapper<BusinessCardVisitorRecordDO> queryWrapper = new QueryWrapper<>();

        // 处理nickname字段，添加别名前缀
        if (query.getNickname() != null && !query.getNickname().isEmpty()) {
            String aliasedNickname = "t3.nickname";
            queryWrapper.like(aliasedNickname, query.getNickname());
        }

        super.sort(queryWrapper, pageQuery);

        IPage<BusinessCardVisitorRecordResp> page = baseMapper.queryVisitorRecordPage(new Page<>(pageQuery
                .getPage(), pageQuery.getSize()), queryWrapper);
        PageResp<BusinessCardVisitorRecordResp> pageResp = PageResp.build(page, super.getListClass());
        pageResp.getList().forEach(this::fill);
        return pageResp;
    }

    @Override
    public BusinessCardVisitorRecordDetailResp get(Long id) {
        return baseMapper.queryVisitorRecordDetail(id);
    }

    @Override
    public Integer getCountByCardId(Long cardId) {
        var result = baseMapper.lambdaQuery()
                .select(BusinessCardVisitorRecordDO::getId)
                .eq(BusinessCardVisitorRecordDO::getCardId, cardId)
                .list();

        return result.size();
    }

    @Override
    public List<CardVisitorResp> getCardVisitorListByCardId(Long cardId, Integer number) {
        return baseMapper.queryVisitorListByCardId(cardId, number);
    }

    @Override
    public VisitorStatisticsRep getVisitorPraise(Long cardId, Integer number) {
        VisitorStatisticsRep rep = new VisitorStatisticsRep();
        rep.setVisitorCount(this.getCountByCardId(cardId));
        rep.setVisitorList(this.getCardVisitorListByCardId(cardId, number));

        return  rep;
    }
}